#include <GL/glut.h>

#include <stdio.h>
#include <math.h>

#include <GL/glui.h>
#include "origin.h"
#include "Snow.h"
#include"launchSite.h"

int
window_width, window_height,

		main_context_menu;

Origin *heightfield;

Snow *snow;

bool isSnow = false;
bool enableTexture = false;
GLuint main_window;

GLUI_Button* b_quit;
GLUI_Checkbox* c_isSnow;
GLUI_Checkbox* c_enableTexture;
GLUI_Spinner* num_snow;
GLUI_Spinner* x_wind;
GLUI_Spinner* y_wind;
GLUI_Spinner* z_wind;
GLUI_Panel *wind;

void ui_set_num_snow(int x) {
	snow->setNum_flakes(num_snow->get_int_val());
}
void ui_set_wind(int x) {
	snow->setWind(x_wind->get_int_val(), y_wind->get_int_val(),
			z_wind->get_int_val());
}

//init functions
void init(char* file) {
	floatbuffer* zbuf;
	heightfield = new Origin(512, 512);
	heightfield->loadmodel(file);
	snow = new Snow(512, 512, heightfield);

	zbuf = heightfield->getdepthbuffer();
	snow->initialize(zbuf);
	snow->init();

	glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
}



void keyboard(unsigned char key, int x, int y) {
	switch (key) {

	case 27:
		exit(0);
		break;

	default:
		if (isSnow)
			snow->keyboard(key, x, y);
		else
			heightfield->keyboard(key, x, y);

	}
}


void mouse(int button, int s, int x, int y) {
	if (isSnow)
		snow->mouse(button, s, x, y);
	else
		heightfield->mouse(button, s, x, y);
}




void motion(int x, int y) {
	if (isSnow)
		snow->motion(x, y);
	else
		heightfield->motion(x, y);

}



void display(void) {
	if (isSnow) {
		snow->display();

	} else
		heightfield->display();
}



void reshape(int width, int height) {
	if (isSnow)
		snow->reshape(width, height);
	else
		heightfield->reshape(width, height);

}



void context_menu(int value) {

	char* name = 0;

	switch (value) {

	case 'a':
		name = "data/al.obj";
		break;
	case 's':
		name = "data/soccerball.obj";
		break;
	case 'f':
		name = "data/flowers.obj";
		break;
	case 'j':
		name = "data/f-16.obj";
		break;
	case 'p':
		name = "data/porsche.obj";
		break;
		break;

	}

	if (name)
		heightfield->loadmodel(name);

	glutPostRedisplay();
}

void myGlutIdel(void) {
	glutSetWindow(main_window);
	glutPostRedisplay();
}

void ui_isSnow(int id) {
	isSnow = !isSnow;

	if (isSnow) {
		floatbuffer* zbuf;

		zbuf = heightfield->getdepthbuffer();
		snow->initialize(zbuf);
		snow->init();
	}
}
void ui_enableTexture(int x) {
	if (c_enableTexture->get_int_val())
		snow->enableFrog(true);
	else
		snow->enableFrog(false);
}

int main(int argc, char * argv[]) {

	//read file
	if (argc > 1)
		init(argv[1]);
	else
		init("data/al.obj");

	//set up glut and glui
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowSize(512 + 150, 512);
	glutInitWindowPosition(100, 100);
	glutInit(&argc, argv);
	main_window = glutCreateWindow("snow simulator");

	GLUI_Master.set_glutIdleFunc(myGlutIdel);
	GLUI_Master.set_glutKeyboardFunc(keyboard);
	GLUI_Master.set_glutMouseFunc(mouse);
	GLUI_Master.set_glutReshapeFunc(reshape);
	glutDisplayFunc(display);
	glutMotionFunc(motion);

	GLUI *glui_subwin = GLUI_Master.create_glui_subwindow(main_window,
			GLUI_SUBWINDOW_RIGHT);

	glui_subwin->set_main_gfx_window(main_window);


	//add glui controls
	b_quit = glui_subwin->add_button("Quit", 0, exit);
	//c_isSnow =
	glui_subwin->add_checkbox("snow", NULL, -1, ui_isSnow);

	c_enableTexture = glui_subwin->add_checkbox("enable dust", NULL, -1,
			ui_enableTexture);
	num_snow = glui_subwin->add_spinner("More/Less snow", GLUI_SPINNER_INT,
			NULL, -1, ui_set_num_snow);
	num_snow->set_speed(400);

	wind = glui_subwin->add_panel("adjust the wind");
	x_wind = glui_subwin->add_spinner_to_panel(wind, "x", GLUI_SPINNER_INT,
			NULL, -1, ui_set_wind);
	y_wind = glui_subwin->add_spinner_to_panel(wind, "y", GLUI_SPINNER_INT,
			NULL, -1, ui_set_wind);
	z_wind = glui_subwin->add_spinner_to_panel(wind, "z", GLUI_SPINNER_INT,
			NULL, -1, ui_set_wind);


	//create a glut menu
	main_context_menu = glutCreateMenu(context_menu);
	glutAddMenuEntry("Pick a model:", 0);
	glutAddMenuEntry("-----------------------------------", 0);
	glutAddMenuEntry("Soccerball", 's');
	glutAddMenuEntry("Al Capone", 'a');
	glutAddMenuEntry("F-16 Jet", 'j');
	glutAddMenuEntry("Dolphins", 'd');
	glutAddMenuEntry("Flowers", 'f');
	glutAddMenuEntry("Porsche", 'p');
	glutAddMenuEntry("Rose", 'r');
	glutAddMenuEntry("-----------------------------------", 0);
	glutAddMenuEntry("Capture Depths and Begin Simulation", 'C');
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	glutMainLoop();

	//should not reach here.
	return -1;
}
